package com.itheima.dao.impl;

import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
import com.itheima.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    ArrayList<Student> students = null;
    @Override
    public List<Student> findAll() throws Exception {
        students = new ArrayList<>();
        Student student = null;
        Connection connection = JdbcUtils.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement("select id,name,age from student");

        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");

            students.add(new Student(id, name, age));
        }
        JdbcUtils.close(connection,preparedStatement,resultSet);
        return students;
    }

   /* public static void main(String[] args) throws Exception{
        List<Student> list = new StudentDaoImpl().findAll();
        System.out.println(list);
    }*/
}
